<?php

namespace app\common\model\dealer;

use think\Hook;
use app\common\model\BaseModel;
use app\common\enum\OrderType as OrderTypeEnum;
use app\common\library\MysicLog;

/**
 * 分销商订单模型
 * Class Apply
 * @package app\common\model\dealer
 */
class Order extends BaseModel
{
    protected $name = 'dealer_order';

    /**
     * 订单模型初始化
     */
    public static function init()
    {
        parent::init();
        // 监听分销商订单行为管理
        $static = new static;
        Hook::listen('DealerOrder', $static);
    }

    /**
     * 订单所属用户
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('app\common\model\User');
    }

    /**
     * 一级分销商用户
     * @return \think\model\relation\BelongsTo
     */
    public function dealerFirst()
    {
        return $this->belongsTo('User', 'first_user_id');
    }

    /**
     * 二级分销商用户
     * @return \think\model\relation\BelongsTo
     */
    public function dealerSecond()
    {
        return $this->belongsTo('User', 'second_user_id');
    }

    /**
     * 三级分销商用户
     * @return \think\model\relation\BelongsTo
     */
    public function dealerThird()
    {
        return $this->belongsTo('User', 'third_user_id');
    }

    /**
     * 订单类型
     * @param $value
     * @return array
     */
    public function getOrderTypeAttr($value)
    {
        $types = OrderTypeEnum::getTypeName();
        return ['text' => $types[$value], 'value' => $value];
    }

    /**
     * 订单详情
     * @param $where
     * @return Order|null
     * @throws \think\exception\DbException
     */
    public static function detail($where)
    {
        return static::get($where);
    }

    /**
     * 订单详情
     * @param $orderId
     * @param $orderType
     * @return Order|null
     * @throws \think\exception\DbException
     */
    public static function getDetailByOrderId($orderId, $orderType)
    {
        return static::detail(['order_id' => $orderId, 'order_type' => $orderType]);
    }


    // +----------------------------------------------------------------------
    // | 计算商品订单分销佣金
    // +----------------------------------------------------------------------

    /**
     * 计算商品的订单分销佣金方案一
     * @param $order
     * @return array
     */
    protected function getCapitalByOrder($order)
    {

        // 分销层级
        $level = 1;
        // 分销订单佣金数据
        $capital = [
            // 订单总金额(不含运费)
            'orderPrice' => bcsub($order['pay_price'], $order['express_price'], 2),
            // 一级分销佣金
            'first_money' => 0.00,
            // 二级分销佣金
            'second_money' => 0.00,
            // 三级分销佣金
            'third_money' => 0.00
        ];

        // 计算分销佣金
        foreach ($order['goods'] as $goods) {
            // 判断商品存在售后退款则不计算佣金
            if ($this->checkGoodsRefund($goods)) {
                continue;
            }

            // 商品实付款金额
            $goodsPrice = min($capital['orderPrice'], $goods['total_pay_price']);

            // 计算商品实际佣金
            $goodsCapital = $this->calculateGoodsCapital($goods, $goodsPrice);
            // 累积分销佣金
            $level >= 1 && $capital['first_money'] += $goodsCapital['first_money'];
            $level >= 2 && $capital['second_money'] += $goodsCapital['second_money'];
            $level == 3 && $capital['third_money'] += $goodsCapital['third_money'];
        }
        return $capital;
    }



    /**
     * 计算商品实际佣金
     * @param $setting
     * @param $goods
     * @param $goodsPrice
     * @return array
     */
    private function calculateGoodsCapital($goods, $goodsPrice)
    {
        // 判断是否开启商品单独分销
//        if ($goods['is_ind_dealer'] == false) {
//            // 全局分销比例
//            return [
//                'first_money' => $goodsPrice * ($setting['first_money'] * 0.01),
//                'second_money' => $goodsPrice * ($setting['second_money'] * 0.01),
//                'third_money' => $goodsPrice * ($setting['third_money'] * 0.01)
//            ];
//        }
        // 商品单独分销
        if ($goods['dealer_money_type'] == 10) {
            // 分销佣金类型：百分比
            return [
                'first_money' => $goodsPrice * ($goods['first_money'] * 0.01),
                'second_money' => $goodsPrice * ($goods['second_money'] * 0.01),
                'third_money' => $goodsPrice * ($goods['third_money'] * 0.01)
            ];
        } else {
            return [
                'first_money' => $goods['total_num'] * $goods['first_money'],
                'second_money' => $goods['total_num'] * $goods['second_money'],
                'third_money' => $goods['total_num'] * $goods['third_money']
            ];
        }
    }





    /**
     * 计算商品的订单分销佣金方案二
     * @param $order
     * @return array
     */
    protected function getCapitalByOrderTow($order,$checkDealerTow)
    {

        // 分销层级
        $level = 1;
        // 分销订单佣金数据
        $capital = [
            // 订单总金额(不含运费)
            'orderPrice' => bcsub($order['pay_price'], $order['express_price'], 2),
            // 一级分销佣金
            'first_money' => bcsub($order['pay_price']*$checkDealerTow/100, 0, 2),
            // 二级分销佣金
            'second_money' => 0.00,
            // 三级分销佣金
            'third_money' => 0.00
        ];

        //$goodsCapital = bcsub($capital['orderPrice']*$checkDealerTow/100, 0, 2);

        // 计算分销佣金
//        foreach ($order['goods'] as $goods) {
//            // 判断商品存在售后退款则不计算佣金
//            if ($this->checkGoodsRefund($goods)) {
//                continue;
//            }
//
//            // 商品实付款金额
//            $goodsPrice = min($capital['orderPrice'], $goods['total_pay_price']);
//
//            // 计算商品实际佣金
//            $goodsCapital = $this->calculateGoodsCapitalTow($goodsPrice,$checkDealerTow);
//            // 累积分销佣金
//            $level >= 1 && $capital['first_money'] += $goodsCapital['first_money'];
//            $level >= 2 && $capital['second_money'] += $goodsCapital['second_money'];
//            $level == 3 && $capital['third_money'] += $goodsCapital['third_money'];
//        }
        return $capital;
    }



    /**
     * 计算商品实际佣金
     * @param $setting
     * @param $goods
     * @param $goodsPrice
     * @return array
     */
    private function calculateGoodsCapitalTow($goodsPrice,$checkDealerTow)
    {


        // 分销佣金类型：百分比
        return [
            'first_money' => $goodsPrice * ($checkDealerTow * 0.01),
            'second_money' => 0,
            'third_money' => 0
        ];

    }



    /**
     * 验证商品是否存在售后
     * @param $goods
     * @return bool
     */
    private function checkGoodsRefund($goods)
    {
        return !empty($goods['refund'])
            && $goods['refund']['type']['value'] == 10
            && $goods['refund']['is_agree']['value'] != 20;
    }




    // +----------------------------------------------------------------------
    // | 发放商品分销佣金
    // +----------------------------------------------------------------------

    /**
     * 发放分销订单佣金
     * @param array|\think\Model $order 订单详情
     * @param int $orderType 订单类型
     * @return bool|false|int
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public static function grantMoney($order, $orderType = OrderTypeEnum::MASTER,$flow_type)
    {

        MysicLog::instance()->info($order, '用户发放商品分销佣金..开始');

        // 订单是否已完成
        if ($order['order_status']['value'] != 30) {
            return false;
        }
        // 订单支付方式
        if ($order['pay_type']['value'] != 20) {
            return false;
        }

        // 佣金结算天数
//        $settleDays = Setting::getItem('settlement', $order['wxapp_id'])['settle_days'];
//        // 判断该订单是否满足结算时间 (订单完成时间 + 佣金结算时间) ≤ 当前时间
//        $deadlineTime = $order['receipt_time'] + ((int)$settleDays * 86400);
//        if ($settleDays > 0 && $deadlineTime > time()) {
//            return false;
//        }
        // 分销订单详情
        $model = self::getDetailByOrderId($order['order_id'], $orderType);


        //MysicLog::instance()->info($model->toArray(), '用户分销订单详情');


        if (!$model || $model['is_settled'] == 1) {
            return false;
        }


        //分销方案一
        if($model['dealer_case']==1){
            // 重新计算分销佣金
            $capital = $model->getCapitalByOrder($order);
            // 发放一级分销商佣金
            $model['first_user_id'] > 0 && User::grantMoney($model['first_user_id'], $capital['first_money'],$model['wxapp_id'],$flow_type);
            // 发放二级分销商佣金
            $model['second_user_id'] > 0 && User::grantMoney($model['second_user_id'], $capital['second_money'],$model['wxapp_id'],$flow_type);
            // 发放三级分销商佣金
            $model['third_user_id'] > 0 && User::grantMoney($model['third_user_id'], $capital['third_money'],$model['wxapp_id'],$flow_type);

            MysicLog::instance()->info([
                'order_price' => $capital['orderPrice'],
                'first_money' => $capital['first_money'],
                'second_money' => $capital['second_money'],
                'third_money' => $capital['third_money'],
                'is_settled' => 1,
                'settle_time' => time()
            ], '用户分销分销方案一参数');


            // 更新分销订单记录
            return $model->save([
                'order_price' => $capital['orderPrice'],
                'first_money' => $capital['first_money'],
                'second_money' => $capital['second_money'],
                'third_money' => $capital['third_money'],
                'is_settled' => 1,
                'settle_time' => time()
            ]);


        //分销方案二
        }elseif($model['dealer_case']==2){

            // 发放一级分销商佣金
            if($model['first_user_id'] > 0 && $model['first_money']>0){
                User::grantMoney($model['first_user_id'], $model['first_money'],$model['wxapp_id'],$flow_type);
                // 更新分销订单记录
                return $model->save([
                    'order_price' => $model['order_price'],
                    'first_money' => $model['first_money'],
                    'second_money' => 0,
                    'third_money' => 0,
                    'is_settled' => 1,
                    'settle_time' => time()
                ]);
            }
            // 发放二级分销商佣金
//            if($model['second_user_id'] > 0 && $model['second_money']>0){
//                User::grantMoney($model['second_user_id'], $model['second_money'],$model['wxapp_id'],$flow_type);
//            }

            // 发放三级分销商佣金
//            if($model['third_user_id'] > 0 && $model['third_money']>0){
//                User::grantMoney($model['third_user_id'], $model['third_money'],$model['wxapp_id'],$flow_type);
//            }



        }

    }


    // +----------------------------------------------------------------------
    // | 发放爆品礼分销订单佣金
    // +----------------------------------------------------------------------

    /**
     * 发放爆品礼分销订单佣金
     * @param array|\think\Model $order 订单详情
     * @param int $orderType 订单类型
     * @return bool|false|int
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public static function grantGiftsMoney($order,$orderType,$flow_type)
    {

        MysicLog::instance()->info("开始", '用户发放分销佣金..开始');
        // 分销订单详情
        $model = self::getDetailByOrderId($order['order_id'],$orderType);

        //MysicLog::instance()->info($model->toArray(), '分销订单详情');
        if (!$model || $model['is_settled'] == 1) {
            return false;
        }
        MysicLog::instance()->info($model['first_money'], '用户发放分销佣金..一级佣金');
        // 发放一级分销商佣金
        $model['first_user_id'] > 0 && User::grantMoney($model['first_user_id'], $model['first_money'],$model['wxapp_id'],$flow_type);
        // 发放二级分销商佣金
        //$model['second_user_id'] > 0 && User::grantMoney($model['second_user_id'], $model['second_money'],$model['wxapp_id'],$flow_type);
        // 发放三级分销商佣金
        //$model['third_user_id'] > 0 && User::grantMoney($model['third_user_id'], $model['third_money'],$model['wxapp_id'],$flow_type);
        // 更新分销订单记录
        return $model->save([
            'is_settled' => 1,
            'settle_time' => time()
        ]);


    }



    // +----------------------------------------------------------------------
    // | 发放储值卡分销订单佣金
    // +----------------------------------------------------------------------


    /**
     * 发放储值卡分销订单佣金
     * @param array|\think\Model $order 订单详情
     * @param int $orderType 订单类型
     * @return bool|false|int
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public static function grantVipMoney($order,$orderType = OrderTypeEnum::MASTER,$flow_type)
    {
        MysicLog::instance()->info("开始", '发放储值卡分销订单佣金');
        // 分销订单详情
        $model = self::getDetailByOrderId($order['order_id'],$orderType);

        //MysicLog::instance()->info($model->toArray(), '发放储值卡分销订单佣金分销订单详情');
        if (!$model || $model['is_settled'] == 1) {
            return false;
        }

        // 发放一级分销商佣金
        $model['first_user_id'] > 0 && User::grantMoney($model['first_user_id'], $model['first_money'],$model['wxapp_id'],$flow_type);
        // 发放二级分销商佣金
        $model['second_user_id'] > 0 && User::grantMoney($model['second_user_id'], $model['second_money'],$model['wxapp_id'],$flow_type);
        // 发放三级分销商佣金
        $model['third_user_id'] > 0 && User::grantMoney($model['third_user_id'], $model['third_money'],$model['wxapp_id'],$flow_type);
        // 更新分销订单记录
        return $model->save([
            'is_settled' => 1,
            'settle_time' => time()
        ]);
    }



    // +----------------------------------------------------------------------
    // | 计算储值卡分销订单佣金
    // +----------------------------------------------------------------------

    /**
     * 计算订单分销佣金---分销方案一
     * @param $order
     * @return array
     */
    protected function getCapitalVipByOrder($order)
    {
        //分红拨比
        $reward_percent = $order['plan']['reward_percent'];

        $level = 1;
        // 分销层级
        //$level = Setting::getItem('basic', $order['order']['wxapp_id'])['level'];

        if($reward_percent>0){
            // 分销订单佣金数据
            $capital = [
                // 订单总金额(不含运费)
                'orderPrice' => bcsub($order['order']['pay_price'], 0, 2),
                // 一级分销佣金
                'first_money' => 0.00,
                // 二级分销佣金
                'second_money' => 0.00,
                // 三级分销佣金
                'third_money' => 0.00
            ];
            // 商品实付款金额
            $goodsPrice = min($capital['orderPrice'], $order['order']['total_price']);

            // 计算商品实际佣金
            $goodsCapital = $this->calculateVipCapital($reward_percent,$goodsPrice);

            // 累积分销佣金
            $level >= 1 && $capital['first_money'] += $goodsCapital['first_money'];
            $level >= 2 && $capital['second_money'] += $goodsCapital['second_money'];
            $level == 3 && $capital['third_money'] += $goodsCapital['third_money'];


        }


        return $capital;
    }



    /**
     * 计算订单分销佣金---分销方案二
     * @param $order
     * @return array
     */
    protected function getCapitalVipByOrderTow($order,$checkDealerTow)
    {

        // 分销层级
        $level = 1;
        //$level = Setting::getItem('basic', $order['order']['wxapp_id'])['level'];

        // 分销订单佣金数据
        $capital = [
            // 订单总金额(不含运费)
            'orderPrice' => bcsub($order['order']['pay_price'], 0, 2),
            // 一级分销佣金
            'first_money' => 0.00,
            // 二级分销佣金
            'second_money' => 0.00,
            // 三级分销佣金
            'third_money' => 0.00
        ];

        // 商品实付款金额
        $goodsPrice = min($capital['orderPrice'], $order['order']['total_price']);

        // 计算商品实际佣金
        $goodsCapital = $this->calculateVipCapital($checkDealerTow,$goodsPrice);

        // 累积分销佣金
        $level >= 1 && $capital['first_money'] += $goodsCapital['first_money'];
        $level >= 2 && $capital['second_money'] += $goodsCapital['second_money'];
        $level == 3 && $capital['third_money'] += $goodsCapital['third_money'];


        return $capital;
    }



    /**
     * 计算商品实际佣金
     * @param $setting
     * @param $goods
     * @param $goodsPrice
     * @return array
     */
    private function calculateVipCapital($reward_percent, $goodsPrice)
    {
        return [
            'first_money' => bcsub($goodsPrice* ($reward_percent * 0.01),0, 2),
            'second_money' => 0,
            'third_money' => 0
        ];

    }




    /**
     * 批量获取订单列表
     * @param $orderIds
     * @param array $with 关联查询
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getListByIds($orderIds, $with = [])
    {
        $data = $this->getListByInArray('order_id', $orderIds, $with);
        return helper::arrayColumn2Key($data, 'order_id');
    }

    /**
     * 批量获取订单列表
     * @param string $field
     * @param array $data
     * @param array $with
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function getListByInArray($field, $data, $with = [])
    {

        return $this->with($with)
            ->where($field, 'in', $data)
           // ->where('is_delete', '=', 0)
            ->select();
    }

    /**
     * 订单商品列表
     * @return \think\model\relation\HasMany
     */
    public function goods()
    {
        $module = self::getCalledModule() ?: 'common';
        return $this->hasMany("app\\{$module}\\model\\vipcard\\Card");
    }



    /**
     * 关联订单收货地址表
     * @return \think\model\relation\HasOne
     */
    public function address()
    {
        $module = self::getCalledModule() ?: 'common';
        return $this->hasOne("app\\{$module}\\model\\OrderAddress");
    }





}
